#!/usr/bin/env bash

set -e # exit when error

NAME='docsearch'
LICENSE="/*! ${NAME} ${VERSION:-UNRELEASED} | © Algolia | github.com/algolia/docsearch */"
DIST_DIR="dist/cdn"
DIST_DIR_NPM="dist/npm"
DIST_FILE="$DIST_DIR/${NAME}.css"
DIST_FILE_MIN="$DIST_DIR/${NAME}.min.css"
DIST_FILE_MAP="$DIST_DIR/${NAME}.min.css.map"

mkdir -p "$DIST_DIR"

# ./dist/cdn/docsearch.css
echo "$LICENSE" > "$DIST_FILE";
sass --style=expanded ./src/styles/main.scss \
  | postcss --use autoprefixer -o "$DIST_FILE"

# copy styles
cp -r src/styles $DIST_DIR_NPM

# ./dist/cdn/docsearch.min.css
postcss "$DIST_FILE" --use cssnano --map "$DIST_FILE_MAP" -o "$DIST_FILE_MIN"

gzip_size=$(gzip -9 < $DIST_FILE_MIN | wc -c | pretty-bytes)
echo "=> $DIST_FILE_MIN gzipped will weight $gzip_size-bytes"
